<?php

include_once ('bn_path.php');

class bn_genralfunc
{
    public $mail;
    protected static $instance;

    function __construct()
    {
        $this->mail = new mail();
    }
    
     public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function Get_ip()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
        {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
        //to check ip is pass from proxy
        {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else
        {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    public function msgbox($msg, $title = '', $secund = 5, $refer = '')
    {
        if (empty($refer))
            $refer = bn_Get_current_URL();

        $msg = str_replace('\n', '<br />', $msg);
        ob_clean();
        ob_start();

        echo "
<body style='background-color: darkgray;'>
<div style='
margin: 0 auto; 
width: 408px;
position: relative;
top:200px'>
<div style='
background-color:blue;
border:2px solid darkblue;
border-top-left-radius:5px;
border-top-right-radius:5px;
width:400px;
padding:3px;
text-align:center;
'>
$title
</div>
<div style='
border:2px solid gray;
margin-top:5px;
text-align:center;
width:385px;
padding:20px 10px 10px 10px;
'>

$msg

<div style='clear: both;'></div>
<a href='$refer' style='
color: #000000; 
background-color: #FF6600; 
border-radius: 3px; 
padding: 0px 8px 2px 8px; 
margin-bottom: 5px;
text-decoration: none;
'>return</a>
</div>
</div>
</body>";
        header("Refresh: $secund; URL=$refer");
        die;
    }

    //hash string
    public function Hash_string($value, $hash = 'sha256')
    {
        return hash($hash, base64_encode($value));
    }

    public function class_property($class, $array)
    {
        $keys = array_keys(get_object_vars($class));
        foreach ($keys as $key)
        {
            if (isset($array[$key]))
                $class->$key = $array[$key];
        }
    }

    public function RandomStrgenerate($length = 10)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++)
        {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    public function date($date = 'now')
    {
        return date('Y-m-d H:i:s', strtotime($date));
    }

    public function editor($theme,$text='')
    {
        $theme->add_script('$( "textarea.editor" ).ckeditor();');

        $theme->add_link(bn_get_relative_path() . root . '/ckeditor/ckeditor.js');
        $theme->add_link(bn_get_relative_path() . root . '/ckeditor/adapters/jquery.js');
        echo "<textarea name='editor' class='editor'>$text</textarea>";
    }

    public function Get_number($str)
    {
        return filter_var($str, FILTER_SANITIZE_NUMBER_INT);
    }
    //email function

    public function valid_mail($email)
    {
        $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-_]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
        return (bool)preg_match($regex, $email);
    }

    public function valid_site($site)
    {

        return (bool)preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*(\.[-a-z0-9+&@#\/%=~_|]{2,4})$/",
            $site);
    }

    public function valid_url($url)
    {
        return (bool)preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",
            $url);
    }

    public function valid_mobile($mob)
    {
        $nu = substr($mob, 0, 2);
        if ($nu == '09' && strlen($mob) == 11)
            return is_numeric($mob);
        elseif ($nu == '+9' && strlen($mob) == 13)
        {
            $mob = substr($mob, 1, strlen($mob) - 1);
            return is_numeric($mob);
        } else
            return false;
    }
}

class mail
{
    public function mail_send($to, $subject, $message, $from = '')
    {
        if ($from != '')
        {
            $header .= "From: $from\r\n";
        }
        mail($to, $subject, $message, $header);
        return 1;
    }


    public function mail_sendHTML($to, $subject, $message, $from = '')
    {
        $headers = 'MIME-Version: 1.0
                    Content-Type: text/html; charset="UTF-8"
                    Content-Transfer-Encoding: 7bit';
        if ($from != '')
        {
            $headers .= "From: $from\r\n";
        }
        mail($to, $subject, $message, $headers);
        return 1;
    }
}

?>